//
// Created by zhupi on 2024/10/18.
//

#include "geometry_table_widget.h"

void PropertiesWidget::InitTable()
{
    table_ = new MyTableWidget(this);
    table_->verticalHeader()->setVisible(false);  // 去除左边的序号
    table_->setTableSize(10, 10);  // 设置为10行10列
    table_->horizontalHeader()->setStyleSheet("background-color: #A9A9A9; font-weight: bold;");  // 设置表头浅灰色
    table_->setHorizontalHeaderLabels(QStringList() << "Name" << "Value" << "Unit" << "Evaluated...");

    CustomDelegate *delegate = new CustomDelegate(table_);
    table_->setItemDelegate(delegate);

    // 布局
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(table_);
    setLayout(layout);

    // 连接cellChanged信号到槽函数
    connect(table_, &MyTableWidget::cellChanged, [&](int row, int column) {
        onCellChanged(row, column, table_);
    });

    //     void ExpressionFinishSg(const QString &expr, bool &isOk);  // 定义信号，包含内容和返回值
    connect(table_, &MyTableWidget::ExpressionFinishSg, [](const QString &expr, bool &isOk) {
        if (expr != "111") {
            isOk = false;
        }
//        onCellChanged(row, column, table_);
    });
}

void PropertiesWidget::SetDefault() {
    // 设置所有单元格不可编辑，除了特定的 Value 列
//    for (int row = 0; row < table_->rowCount(); ++row) {
//        for (int col = 0; col < table_->columnCount(); ++col) {
//            if (!((row == 0 && col == 1) || (row == 1 && col == 1) || (row == 3 && col == 1) || (row == 5 && col == 1))) {
//                QTableWidgetItem *item = table_->item(row, col);
//                if (!item) {
//                    item = new QTableWidgetItem();
//                    table_->setItem(row, col, item);
//                }
//                item->setFlags(item->flags() & ~Qt::ItemIsEditable);
//            }
//        }
//    }

    // 设置所有行的高度为 24
    for (int row = 0; row < table_->rowCount(); ++row) {
        table_->setRowHeight(row, 24);
    }

    // 设置列宽
    table_->setColumnWidth(0, 80); // Name 列
    table_->setColumnWidth(1, 150); // Value 列
    table_->setColumnWidth(2, 80); // Unit 列
    table_->setColumnWidth(3, 80); // Evaluated... 列
}

void PropertiesWidget::InitTest(int row, const QVector<CellWidgetStruct> &vec) {

    for (int index = 0; index < vec.size(); index++) {
        SetCellWidet(row, index, vec[index]);
    }
}

void PropertiesWidget::SetCellWidet(int row, int column, const CellWidgetStruct &data)
{
    switch (data.type) {
        case CellType::TextEditable: {
            QTableWidgetItem *item = new QTableWidgetItem(data.name);
            table_->setItem(row, column, item);
            item->setFlags(item->flags() | Qt::ItemIsEditable);  // 可编辑
            break;
        }
        case CellType::TextDisEditable:{
            QTableWidgetItem *item = new QTableWidgetItem(data.name);
            table_->setItem(row, column, item);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
            break;
        }
        case CellType::ComboxEditable:
            break;
        case CellType::MaterialCombox: {
            QTableWidgetItem *item = new QTableWidgetItem(data.name);
            table_->setItem(row, column, item);
            item->setData(Qt::UserRole, static_cast<int>(CellType::MaterialCombox)); // 使用UserRole存储数据类型
//            ComboBoxDelegate *comboBoxDelegate = new ComboBoxDelegate(this);
//            table_->setItemDelegateForRow(row, comboBoxDelegate);
            break;
        }
        case CellType::GroupCombox: {
            QTableWidgetItem *item = new QTableWidgetItem(data.name);
            table_->setItem(row, column, item);
            item->setData(Qt::UserRole, static_cast<int>(CellType::GroupCombox)); // 使用UserRole存储数据类型
//            ComboBoxDelegate *comboBoxDelegate = new ComboBoxDelegate(this);
//            table_->setItemDelegateForRow(row, comboBoxDelegate);
            break;
        }
        case CellType::OrientationCombox: {
            QTableWidgetItem *item = new QTableWidgetItem(data.name);
            table_->setItem(row, column, item);
            item->setData(Qt::UserRole, static_cast<int>(CellType::OrientationCombox)); // 使用UserRole存储数据类型
//            ComboBoxDelegate *comboBoxDelegate = new ComboBoxDelegate(this);
//            table_->setItemDelegateForRow(row, comboBoxDelegate);
            break;
        }
        case CellType::Color:{
            QPushButton *colorButton = new QPushButton();
            colorButton->setStyleSheet("background-color: lightgreen");
            table_->setCellWidget(row, column, colorButton);
            connect(colorButton, &QPushButton::clicked, [=]() {
                QColor color = QColorDialog::getColor(Qt::green, this, "Choose Color");
                if (color.isValid()) {
                    colorButton->setStyleSheet(QString("background-color: %1").arg(color.name()));
                }
            });
            break;
        }

        case CellType::SolveInsideStatus:{
//            table_->setItem(2, 0, new QTableWidgetItem("Solve Inside"));
            QCheckBox *solveCheckBox = new QCheckBox();
            solveCheckBox->setStyleSheet("margin: auto;");
            solveCheckBox->setChecked(true);
            table_->setCellWidget(row, column, solveCheckBox);
            break;
        }
        case CellType::TransParent: {
            QPushButton *transparentButton = new QPushButton("0");
            table_->setCellWidget(row, column,  transparentButton);
            connect(transparentButton, &QPushButton::clicked, [&,transparentButton]() {
                bool ok {false};
                int transparency = QInputDialog::getInt(this, "Set Transparency", "Transparency:", 0, 0, 100, 1, &ok);
                if (ok) {
                    transparentButton->setText(QString::number(transparency));
                }
            });
            break;
        }

        default: {
            table_->setItem(row, column, new QTableWidgetItem);
        }
            break;
    }
}
